#include "MonsterCacheData.h"

MonsterCacheData::MonsterCacheData()
	: m_maxCount(0)
{
}

MonsterCacheData::~MonsterCacheData()
{
	Clear();
}

bool MonsterCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void MonsterCacheData::Clear()
{
	_safe_guard_begin;

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool MonsterCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./data/hero/monsterAttr.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> data/hero/monster.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();

	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}

void MonsterCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		MonsterInfo monsterinfo;

		monsterinfo.mId = jm["heros_id"].asUInt();
		strcpy(monsterinfo.mName, jm["name"].asString().c_str());
		strcpy(monsterinfo.mDescribe, jm["describe"].asString().c_str());
		strcpy(monsterinfo.mInfo, jm["info"].asString().c_str());
		strcpy(monsterinfo.mIconName, jm["icon_name"].asString().c_str());
		strcpy(monsterinfo.mModel, jm["model"].asString().c_str());
		monsterinfo.mReleased = jm["released"].asUInt();
		monsterinfo.mIsIcon = jm["is_icon"].asUInt();

		Json::Value skillId = jm["skill_id"];
		uint32_t sz = skillId.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			_safe_check(skillId[i]);

			if (skillId[i].asUInt() > 0)
			{
				monsterinfo.mSkillId[i] = skillId[i].asUInt();
			}
		}

		
		monsterinfo.mGrade = jm["grade"].asUInt();

		monsterinfo.mHp = jm["hp"].asDouble();
		monsterinfo.mLvHp = jm["lv_hp"].asDouble();
		monsterinfo.mAttack = jm["attack"].asDouble();
		monsterinfo.mLvAttack = jm["lv_attack"].asDouble();
		monsterinfo.mArmor = jm["armor"].asDouble();
		monsterinfo.mLvArmor = jm["lv_armor"].asDouble();
		monsterinfo.mMagicResist = jm["magic_resist"].asDouble();
		monsterinfo.mLvMagicResist = jm["lv_resist"].asDouble();
		monsterinfo.mCritical = jm["critical"].asDouble();
		monsterinfo.mLvCritical = jm["lv_critical"].asDouble();
		monsterinfo.mDodge = jm["dodge"].asDouble();
		monsterinfo.mLvDodge = jm["lv_dodge"].asDouble();
		monsterinfo.mHitRatio = jm["hit_ratio"].asDouble();
		monsterinfo.mLvHitRatio = jm["lv_ratio"].asDouble();
		monsterinfo.mArmorPenetration = jm["armor_penetration"].asDouble();
		monsterinfo.mLvArmorPenetration = jm["lv_armorpenetration"].asDouble();
		monsterinfo.mMagicPenetration = jm["magic_penetration"].asDouble();
		monsterinfo.mLvMagicPenetration = jm["lv_magicpenetration"].asDouble();
		monsterinfo.mSuckBlood = jm["suck_blood"].asDouble();
		monsterinfo.mLvSuckBlood = jm["lv_suckblood"].asDouble();
		monsterinfo.mTenacity = jm["tenacity"].asDouble();
		monsterinfo.mLvTenacity = jm["lv_tenacity"].asDouble();
		monsterinfo.mMovementSpeed = jm["movement_speed"].asDouble();
		monsterinfo.mAttackSpeed = jm["attack_speed"].asDouble();
		monsterinfo.mStrikingDistance = jm["striking_distance"].asDouble();

		m_mapCacheData[id] = monsterinfo;
	}

	_safe_guard_end;
}

MonsterInfo* MonsterCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return &(itor->second);
	}
	_safe_guard_end;

	return nullptr;
}

bool MonsterCacheData::GetDataJsonString(uint32_t id, Json::Value& _jm)
{
	_safe_guard_begin;

	MonsterInfo* p = GetData(id);
	if (nullptr == p)
	{
		LOG_ERROR("default","Error11");
		return false;
	}

	_jm.clear();

	_jm["id"] = Json::Value(p->mId);
	_jm["types"] = Json::Value(p->mTypes);
	_jm["name"] = Json::Value(p->mName);
	_jm["describe"] = Json::Value(p->mDescribe);
	_jm["info"] = Json::Value(p->mInfo);
	_jm["icon_name"] = Json::Value(p->mIconName);
	_jm["model"] = Json::Value(p->mModel);
	_jm["released"] = Json::Value(p->mReleased);
	_jm["is_icon"] = Json::Value(p->mIsIcon);

	Json::Value skillId;
	for (int i = 0; i < 7; ++i)
	{
		if (p->mSkillId[i])
		{
			skillId.append(p->mSkillId[i]);
		}
	}
	_jm["skill_id"] = skillId;

	_jm["grade"] = Json::Value(p->mGrade);
	_jm["hp"] = Json::Value(p->mHp);
	_jm["lv_hp"] = Json::Value(p->mLvHp);
	_jm["attack"] = Json::Value(p->mAttack);
	_jm["lv_attack"] = Json::Value(p->mLvAttack);
	_jm["armor"] = Json::Value(p->mArmor);
	_jm["lv_armor"] = Json::Value(p->mLvArmor);
	_jm["magic_resist"] = Json::Value(p->mMagicResist);
	_jm["lv_resist"] = Json::Value(p->mLvMagicResist);
	_jm["critical"] = Json::Value(p->mCritical);
	_jm["lv_critical"] = Json::Value(p->mLvCritical);
	_jm["dodge"] = Json::Value(p->mDodge);
	_jm["lv_dodge"] = Json::Value(p->mLvDodge);
	_jm["hit_ratio"] = Json::Value(p->mHitRatio);
	_jm["lv_ratio"] = Json::Value(p->mLvHitRatio);
	_jm["armor_penetration"] = Json::Value(p->mArmorPenetration);
	_jm["lv_armorpenetration"] = Json::Value(p->mLvArmorPenetration);
	_jm["magic_penetration"] = Json::Value(p->mMagicPenetration);
	_jm["lv_magicpenetration"] = Json::Value(p->mLvMagicPenetration);
	_jm["suck_blood"] = Json::Value(p->mSuckBlood);
	_jm["lv_suckblood"] = Json::Value(p->mLvSuckBlood);
	_jm["tenacity"] = Json::Value(p->mTenacity);
	_jm["lv_tenacity"] = Json::Value(p->mLvTenacity);

	_jm["movement_speed"] = Json::Value(p->mMovementSpeed);
	_jm["attack_speed"] = Json::Value(p->mAttackSpeed);
	_jm["striking_distance"] = Json::Value(p->mStrikingDistance);

	_safe_guard_end;

	return true;
}

uint32_t MonsterCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}